package com.wushi.lenovo.asia5b.app_ui.credit.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.credit.BaseHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.TopupHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.TransactionHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.TransferHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.WithdrawHisModel;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TransactionHisType;
import com.wushi.lenovo.asia5b.app_utils.WFileUtil;

import java.io.File;
import java.util.List;

import timber.log.Timber;

/**
 * Created by zhangyuncai on 2019/1/21.
 */
public class TransHisListAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public TransHisListAdapter(@Nullable List<MultiItemEntity> data) {
        super(data);
        addItemType(TransactionHisType.all.ordinal(), R.layout.item_transhis_topup_adapter);
        addItemType(TransactionHisType.topup.ordinal(), R.layout.item_transhis_topup_adapter);
        addItemType(TransactionHisType.withdraw.ordinal(), R.layout.item_transhis_topup_adapter);
        addItemType(TransactionHisType.credittrans.ordinal(), R.layout.item_transhis_topup_adapter);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        TextView tv_his_view = helper.getView(R.id.tv_his_view);
        ImageView iv_his_downicon = helper.getView(R.id.iv_his_downicon);
        tv_his_view.setVisibility(View.INVISIBLE);
        iv_his_downicon.setVisibility(View.INVISIBLE);
        //pdf下载地址是否存在
        if (item instanceof BaseHisModel) {
            BaseHisModel baseHisModel = (BaseHisModel) item;
            String filePath = fileExists(mContext, baseHisModel.getReference_number());
            if (TextUtils.isEmpty(filePath)) {
                iv_his_downicon.setVisibility(View.VISIBLE);
            } else {
                tv_his_view.setVisibility(View.VISIBLE);
            }
        }
        if (item instanceof TopupHisModel) {
            TopupHisModel model = (TopupHisModel) item;
            //tv_his_time
            helper.setText(R.id.tv_his_time, model.getCreated_at());
            //tv_his_transtype
            helper.setText(R.id.tv_his_transtype, model.getPayment_method());
            //tv_his_code
            helper.setText(R.id.tv_his_code, String.format("(%s)", model.getReference_number()));
            //tv_his_amount
            helper.setText(R.id.tv_his_amount, model.getAmount());
            //tv_his_status
            helper.setText(R.id.tv_his_status, model.getStatus());
        } else if (item instanceof TransferHisModel) {
            TransferHisModel model = (TransferHisModel) item;
            //tv_his_time
            helper.setText(R.id.tv_his_time, model.getCreated_at());
            //tv_his_transtype
            helper.setText(R.id.tv_his_transtype, model.getDescription());
            //tv_his_code
            helper.setText(R.id.tv_his_code, String.format("(%s)", model.getReference_number()));
            //tv_his_amount
            helper.setText(R.id.tv_his_amount, model.getAmount());
            //tv_his_status
            helper.setText(R.id.tv_his_status, model.getStatus());
        } else if (item instanceof WithdrawHisModel) {
            WithdrawHisModel model = (WithdrawHisModel) item;
            //tv_his_time
            helper.setText(R.id.tv_his_time, model.getCreated_at());
            //tv_his_transtype
            helper.setText(R.id.tv_his_transtype, model.getHolded_bank());
            //tv_his_code
            helper.setText(R.id.tv_his_code, String.format("(%s)", model.getReference_number()));
            //tv_his_amount
            helper.setText(R.id.tv_his_amount, model.getWithdraw_amount());
            //tv_his_status
            helper.setText(R.id.tv_his_status, model.getStatus());
        } else if (item instanceof TransactionHisModel) {
            TransactionHisModel model = (TransactionHisModel) item;
            //tv_his_time
            helper.setText(R.id.tv_his_time, model.getCreated_at());
            if (TextUtils.equals("Opening Balance", model.getType())) {
                helper.setGone(R.id.tv_his_code, false);
                helper.setText(R.id.tv_his_status, "");
                helper.setText(R.id.tv_his_amount, model.getAmount());
                helper.setText(R.id.tv_his_transtype, "Opening Balance");
                helper.setTextColor(R.id.tv_his_transtype, ContextCompat.getColor(mContext, R.color.common_text_gray));

                tv_his_view.setVisibility(View.GONE);
                iv_his_downicon.setVisibility(View.GONE);
            } else {
                helper.setGone(R.id.tv_his_code, true);

                //tv_his_transtype
                helper.setTextColor(R.id.tv_his_transtype, ContextCompat.getColor(mContext, R.color.colorPrimaryDark));
                helper.setText(R.id.tv_his_transtype, model.getType());
                //tv_his_code
                helper.setText(R.id.tv_his_code, String.format("(%s)", model.getReference_number()));
                //tv_his_amount
                helper.setText(R.id.tv_his_amount, model.getAmount() + model.getStatus());
                //tv_his_status
                helper.setText(R.id.tv_his_status, model.getBalance());
            }
        }

    }

    //这里不需要权限,文件是否存在,先判断pdf文件,再判断img文件
    private String fileExists(Context context, String reference_number) {
        String pdfPath = WFileUtil.createPdfPath(context, reference_number);
        File file = new File(pdfPath);
        Timber.d("--->>>>>>>>---pdfPath是否存在:" + file.exists());
        if (file.exists()) {
            return pdfPath;
        } else {
            String imgPath = WFileUtil.createImagePath(context, reference_number);
            file = new File(imgPath);
            if (file.exists()) {
                return imgPath;
            }
        }
        return null;
    }
}
